// pages/companyCertification/companyCertification.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key: 1,
    fontImg: '../../images/zhengmian.jpg',
    reverseImg: '../../images/fanmian.jpg',
    realFontImg: '../../images/zhengmian.jpg',
    realReverseImg: '../../images/fanmian.jpg',
    enterpriseCertificate: [], //企业证件照片
    officeEnvironment: [], //办公环境照片
    certificates: ['身份证', '军官证', '学生证'],
    m_pic1: '',
    m_pic2: '',
    m_pic3: [],
    m_pic4: [],
  },

  // 认证切换
  authenticationChange: function (e) {
    this.setData({
      key: e.currentTarget.dataset.key
    })
  },

  // 上传法人证件
  uploadCompanyImg: function (e) {
    let that = this
    let index = e.currentTarget.dataset.index
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        if (index == 1) {
          that.uploadImg(tempFilePaths, 'm_pic1')
          that.setData({
            fontImg: tempFilePaths
          })
        } else {
          that.uploadImg(tempFilePaths, 'm_pic2')
          that.setData({
            reverseImg: tempFilePaths
          })
        }
      }
    })

  },

  // 企业证件
  uploadCertificate: function (e) {
    let that = this
    let enterpriseCertificate = that.data.enterpriseCertificate
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        enterpriseCertificate.push(tempFilePaths)
        that.setData({
          enterpriseCertificate
        })
        that.uploadImg(tempFilePaths, 'm_pic3')
      }
    })
  },

  // 删除证件
  deleteImg: function (e) {
    let enterpriseCertificate = this.data.enterpriseCertificate
    let index = e.target.dataset.index
    enterpriseCertificate.splice(index, 1)
    this.data.m_pic3.splice(index, 1)
    this.setData({
      enterpriseCertificate
    })
  },

  // 预览证件
  previewImage: function (e) {
    let current = e.target.dataset.src
    let enterpriseCertificate = this.data.enterpriseCertificate

    wx.previewImage({
      urls: enterpriseCertificate,
      current
    })
  },

  // 办公环境证件
  uploadOfficeCertificate: function (e) {
    let that = this
    let officeEnvironment = that.data.officeEnvironment
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        officeEnvironment.push(tempFilePaths)
        that.setData({
          officeEnvironment
        })
        that.uploadImg(tempFilePaths, 'm_pic4')
      }
    })
  },

  // 删除环境证件
  deleteOfficeImg: function (e) {
    let officeEnvironment = this.data.officeEnvironment
    let index = e.target.dataset.index
    officeEnvironment.splice(index, 1)
    this.data.m_pic4.splice(index, 1)
    this.setData({
      officeEnvironment
    })
  },

  // 预览环境证件
  previewOfficeImage: function (e) {
    let current = e.target.dataset.src
    let officeEnvironment = this.data.officeEnvironment

    wx.previewImage({
      urls: officeEnvironment,
      current
    })
  },

  // 上传实名证件
  uploadPersonImg: function (e) {
    let that = this
    let index = e.currentTarget.dataset.index
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        if (index == 1) {
          that.setData({
            realFontImg: tempFilePaths
          })
          that.uploadImg(tempFilePaths, 'm_pic1')
        } else {
          that.setData({
            realReverseImg: tempFilePaths
          })
          that.uploadImg(tempFilePaths, 'm_pic2')
        }
      }
    })
  },

  // 证件类型
  certificatesChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },

  // 公司认证
  companySubmit: function (e) {
    let that = this
    let m_pic3 = that.data.m_pic3
    let m_pic4 = that.data.m_pic4
    let obj = e.detail.value
    obj.m_pic1 = that.data.m_pic1
    obj.m_pic2 = that.data.m_pic2
    m_pic3 = m_pic3.toString().replace(/,/g, '|')
    m_pic4 = m_pic4.toString().replace(/,/g, '|')
    obj.m_pic3 = m_pic3
    obj.m_pic4 = m_pic4
    obj.token = wx.getStorageSync('token')
    if (obj.m_company == '' || obj.m_company_address == '' || obj.m_company_address == '' || obj.m_legal_person == '' || obj.m_link_name == '' || obj.m_link_phone == '' || obj.m_phone == '' || obj.m_pic1 == '' || obj.m_pic2 == '' || obj.m_pic3 == '' || obj.m_pic4 == '') {
      wx.showToast({
        title: '请完善资料..',
        icon: 'none'
      })
    } else {
      wx.showLoading({
        title: '正在提交...',
        mask: true
      })
      app.post('Real/company', obj).then(res => {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '提交成功,请等待审核..',
          showCancel: false,
          success: res => {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })
      })
    }
  },

  // 个人雇主认证
  personSubmit: function (e) {
    let that = this
    let obj = e.detail.value
    obj.m_pic1 = that.data.m_pic1
    obj.m_pic2 = that.data.m_pic2
    obj.token = wx.getStorageSync('token')
    if (obj.m_pic1 == '' || obj.m_pic2 == '' || obj.m_name == ''|| obj.m_card == ''|| obj.m_phone == '')

      wx.showToast({
        title: '请完善资料..',
        icon: 'none'
      })
    wx.showLoading({
      title: '正在提交...',
      mask: true
    })
    app.post('Real/employer', obj).then(res => {
      wx.hideLoading()
      wx.showModal({
        title: '提示',
        content: '提交成功,请等待审核..',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })
    })



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //多张图片上传
  uploadImg(data, imgList) {
    let that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    var imgSrc = ''
    wx.uploadFile({
      url: 'https://wx.zhonghuazhengxin.com/api/Upload/pic',
      filePath: data[i],
      name: 'file',
      formData: null,
      success: (resp) => {
        success++;
        console.log(resp)
        console.log(i);
        let img = JSON.parse(resp.data)
        imgSrc = img.result_data
        if (imgList === 'm_pic3') {
          that.data.m_pic3.push(imgSrc)
        }
        if (imgList === 'm_pic4') {
          that.data.m_pic4.push(imgSrc)
        }
        //这里可能有BUG，失败也会执行这里
      },
      fail: (res) => {
        fail++;
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++;
        if (i == data.length) { //当图片传完时，停止调用 
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
        } else { //若图片还没有传完，则继续调用函数
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadImg(data, imgList);
        }
        if (imgList == 'm_pic1' || imgList == 'm_pic2') {
          that.setData({
            [imgList]: imgSrc
          })
        }

      }
    });
  },

})